var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';

// import { $wuxRefresher } from '../../components/wux';
import { $wuxPrompt } from '../../components/wux';

var app = getApp();
var that;

const sliderWidth = 96
let  pageInex = 1;

Page({
  data: { 
    tabIndex:'0',
    user:null,
    activeIndex: '0',
    sliderOffset: 0,
    sliderLeft: 0,
    list : [],
    isLoadMore : false,
    isEnd : false,
    isEmpty:false,

    htopen:false,
    propen:false,
    navs:false,
    hotDefault:'所有数据',
    hot_choseIndex : 0,
    isfull:false,
    isAll:0,
  	chose:0,
  	kbnow:0,
  	kbyd:0,
  	kbxf:0

  },
  onShareAppMessage: function () {
      return app.getShare();
  },
  onPullDownRefresh:function(){
   that.requestData();
  },
  onLoad: function (options) {
    that = this;
    app.getUserInfo()
   .then(res => {
       that.setData({
           user:res
       })
   })

    if( !util.judge.isNull( options.type) ){
      that.data.hot_choseIndex=1;
    }
    that.data._sdesc="活跃人数";
    wx.setNavigationBarTitle( { title: that.data._sdesc });
 
	
	
    $wuxPrompt.init('msg1', {
      title: '暂时没有相关数据', 
    }).show()

    

    that.setData({
      ht : ["所有数据","获取Kb的数据"]
    });

    that.requestData();
	
  
  }
  ,
  hot_action: function(e) {
      var s;
      if(that.data.htopen) {
          s = false;
      } else {
          s = true;
      }
      that.setData({
          chose: 1,
          navs:s,
          isfull:s,
          htopen:!that.data.htopen,
          propen:false
      });
  },
    //热门
  hot_type:function (e) {
      var dataset = e.currentTarget.dataset;
      var s;
      if(that.data.htopen) {
          s = false;
      } else {
          s = true;
      }
      that.setData({
          hot_choseIndex:dataset.choseindex,
          htopen:!that.data.htopen,
          navs:s,
          isfull:s,
          hotDefault:that.data.ht[dataset.choseindex]
      });
      that.requestData();
  },
  hidebg : function () {
    that.setData({
        isfull:false,
        htopen:false,
        propen:false,
        navs:false
    });
  },
  requestData: function() {
    // util.alert(that.data.hot_choseIndex);
    util.showLoading();

    pageInex = 1;
    that.setData({
        list : [],
        isLoadMore : false,
        isEnd : false,
        isEmpty:false
    });

    //获取用户的个人信息 包括读取接口 url_day_pm
    var url = config.url_user_get;
    var params = {
        id:app.globalData.user.uid
    }
    wxRequest.postRequest(url, params)
    .then(res=>{

        res.dkuser = util.toobj(res.dkuser);
        console.log(res);
        that.setData({
            kbnow:res.dkuser.kbnow
        });
        if(res.dkuser && res.dkuser.success ){
            that.setData({
                kbyd:res.user.ydkb,
                kbxf:res.dkuser.kball
            });
        }
        that.loadData();
    })
    .catch(res => {
        util.alert_catch(res);
    })
    .finally(function (res) {
        util.hideLoading();
        wx.stopPullDownRefresh();
    });
      
  },
  // touchstart(e) {
  //   that.refresher.touchstart(e)
  // }, 
  // touchmove(e) {
  //   that.refresher.touchmove(e)
  // }, 
  // touchend(e) {
  //   that.refresher.touchend(e)
  // }, 
  changeTab:function(e) {
      console.log(e)
      var index = e.currentTarget.dataset.tabIndex;
      switch (index){
          case '0':
              that.setData({
                  tabIndex:index,
              });
              break;
          case '1':
              that.setData({
                  tabIndex:index,
              });
              break;
      }
  },

  onReady : function(){

  },
  // onReachBottom
  onReachBottom : function(e,a){
      //加载更多
  },
  
  onForwardColumn : function(){
      wx.navigateTo({
        url : "../column/column"
      });
  },

  onPreImage : function(e){
      let src = e.currentTarget.dataset.imgSrc;
      Util.UI.preImage(src);
  },
  loadData: function(){

    that.setData({
      isLoadMore : true
    });

    var code= "kb";
    //if(that.data.hot_choseIndex ==1){
    //  code ="kb";
    //}

    var url = config.url_hyd_user_day;
    var params = {
      pageIndex: pageInex,
      pageSize: app.globalData.defaultPageSize,
      code:code
    }

    wxRequest.postRequest(url, params)
    .then(res=>{

        for(var item in res) {
            res[item].udate = res[item].udate.substring(0,10);
        }

        var data =res;// that.data.list.concat(res);   
        that.setData({
            list : data,
            isLoadMore : false,
            isEnd : true
        });
      //}
      
      //if(res.length < app.globalData.defaultPageSize || res.length == 0 ){
      //  that.setData({
      //    isLoadMore : false,
      //    isEnd : true
      //  });
      //}

    })
    .catch(res => {
      util.alert(res.message);
    })
    .finally(function (res) {
      util.hideLoading();
      wx.stopPullDownRefresh();
    });
  }
  
})


